﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YAGMRC.Core.Commands
{
    public class AuthenticateCommand : ICommand<string>, IResult<AuthenticateCommandResult>
    {
        #region Constructor

        private AuthenticateCommand()
        {
        }

        internal AuthenticateCommand(Resolver resolver, Action<AuthenticateCommandResult> authenticated)
        {
            m_Resolver = resolver;
            m_Authenticated = authenticated;
            this.Result = new AuthenticateCommandResult();
        }

        #endregion Constructor

        private Resolver m_Resolver;
        private Action<AuthenticateCommandResult> m_Authenticated;

        public void Execute(string parameter)
        {
            WebDownload executeWeb = m_Resolver.Get<WebDownload>();
            string webResult = executeWeb.Execute<string>("AuthenticateUser?authKey=" + parameter);

            AuthenticateCommandResult acr = new AuthenticateCommandResult((null != webResult) && (!"null".Equals(webResult)), parameter, webResult);

            m_Authenticated(acr);

            this.Result = acr;
        }

        public bool CanExecute()
        {
            return true;
        }

        public AuthenticateCommandResult Result
        {
            get;
            private set;
        }
    }
}